package com.lanyou.cook.web;


import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.beanutils.DynaBean;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.lanyou.cook.common.Constants;
import com.lanyou.cook.entity.domain.SlaveServerLog;
import com.lanyou.cook.repository.SlaveServerLogRepository;
import com.lanyou.cook.util.DateUtil;
import com.lanyou.cook.web.param.SlaveServerLogParam;

/**
 * 节点运行日志相关业务控制层
 * @author 曾族根
 *
 */
@RestController
@RequestMapping(Constants.BASE_URL_API + "/slaveServerLogs")
public class SlaveServerLogController {

	@Autowired
	private SlaveServerLogRepository slaveServiceLogRepository;
	
	/**
	 * 查询特定条件下的节点运行日志集合
	 * @param param	类型:SlaveServerLogParam 待查询的节点运行日志参数实体
	 * @param pageable 类型:Pageable 待查询的分页参数实体
	 * @return
	 */
	@RequestMapping("/search/findByExample")
	@RequiresPermissions(value={"nodeRunLog:view"})
	public Page<SlaveServerLog> findByExample(SlaveServerLogParam param, Pageable pageable) {
		
		Specification<SlaveServerLog> spec = new Specification<SlaveServerLog>() {
			@Override
			public Predicate toPredicate(Root<SlaveServerLog> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicatesList = new ArrayList<>();
				if(param.getId()!=null){
					predicatesList.add(cb.equal(root.get("kettleSlaveServer").get("id"), param.getId()));
				}
				if (param.getBeginOfCreateTime()==null) {
					param.setBeginOfCreateTime("1900-1-1 00:00:00");
				}
				
				DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				try {
					Date beginDate = sdf.parse(param.getBeginOfCreateTime());
					predicatesList.add(cb.greaterThan(root.get("createTime"), beginDate));
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				if (param.getEndOfCreateTime()==null) {
					param.setEndOfCreateTime("2999-01-01 00:00:00");
				}
				
				try {
					Date endDate = sdf.parse(param.getEndOfCreateTime());
					predicatesList.add(cb.lessThan(root.get("createTime"), endDate));
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				
				query.where(predicatesList.toArray(new Predicate[predicatesList.size()]));
				return null;
			}
		};
		
		return slaveServiceLogRepository.findAll(spec, pageable);
	}
}